<template>
  <div class="file">
    <el-row>
      <el-button type="primary" @click="handleSelectFolder" icon="el-icon-folder">选择文件夹</el-button>
      <el-button type="primary" @click="handleClearFolder" icon="el-icon-folder-remove">清空路径</el-button>
    </el-row>
    <el-row>
      <div class="url-wrap">
        <el-input v-model="url" placeholder="请选择文件夹"></el-input>
      </div>
    </el-row>
    <el-row>
      <p>批量添加代码：</p>
      <el-input type="textarea" :rows="8" placeholder="请输入代码" v-model="code"></el-input>
    </el-row>
    <el-row style="margin-top: 20px">
      <el-button type="success" @click="handleAddCode" icon="el-icon-plus">添加</el-button>
    </el-row>
  </div>
</template>

<script>
export default {
  data() {
    return {
      url: '',
      code: '',
    };
  },
  methods: {
    handleSelectFolder() {
      const result = window.utools.showOpenDialog({
        properties: ['openDirectory'],
      });
      const [selectUrl] = result;
      this.url = selectUrl;
    },
    handleClearFolder() {
      this.url = '';
    },
    handleAddCode() {
      const result = window.addFileFs(this.url, this.code);
      if (result) {
        this.$notify.error({
          message: '添加成功',
        });
      } else {
        this.$notify.error({
          message: '添加失败',
        });
      }
    },
  },
};
</script>
